<?php
require_once 'phpmoko/lib/common.php';

require_once 'phpmoko/ui/Form/FormBase.php';

/**
 * A standard single-line text box.
 *
 * @author BorSanZa
 */
class TextBox extends FormBase
{
	/**
	 * Enter description here
	 *
	 * @var int
	 */
	private $maxCharacters = null;

	/**
	 * Enter description here
	 *
	 * @var int
	 */
	private $maxLength = null;

	/**
	 * Enter description here
	 *
	 * @var string
	 */
	private $value = null;

	/**
	 * Enter description here
	 *
	 * @var bool
	 */
	private $focused = false;

	/**
	 * Enter description here
	 *
	 * @var bool
	 */
	private $enabled = true;

	/**
	 * Creates an empty text box
	 */
	function __construct()
	{
		parent::__construct();

		$this->setStyleName('pmk-TextBox');
	}

	/**
	 *
	 * @return the $maxCharacters
	 */
	public function getMaxCharacters()
	{
		return $this->maxCharacters;
	}

	/**
	 * Enter description here
	 *
	 * @param bool $mode
	 */
	public function setEnabled($mode)
	{
		$this->enabled = $mode;
	}

	/**
	 * Enter description here
	 *
	 * @param int $maxCharacters
	 */
	public function setMaxCharacters($maxCharacters)
	{
		$this->maxCharacters = $maxCharacters;
	}

	/**
	 * Gets the maximum allowable length of the text box.
	 *
	 * @return number
	 */
	function getMaxLength()
	{
		return $this->maxLength;
	}

	/**
	 * Sets the maximum allowable length of the text box.
	 *
	 * @param int $maxLength
	 */
	function setMaxLength($maxLength)
	{
		$this->maxLength = $maxLength;
	}

	/**
	 * Enter description here
	 *
	 * @return string
	 */
	public function getValue()
	{
		return $this->value;
	}

	/**
	 * Enter description here
	 *
	 * @param string $value
	 */
	function setValue($value)
	{
		$this->value = $value;
	}

	/**
	 * Enter description here
	 *
	 * @return string
	 */
	protected function getType()
	{
		return 'text';
	}

	/**
	 *
	 * @param bool $focused
	 */
	public function setFocus($focused)
	{
		$this->focused = $focused;
	}

	/*
	 * (non-PHPdoc) @see Widget::toString()
	 */
	function toString()
	{
		$str = '<input';

		if (!$this->enabled)
			$str .= ' disabled';

		$str .= ' id="' . $this->id . '"';
		if ($this->focused) {
			require_once 'phpmoko/externals/JQuery.php';
			JQuery::activate();
			RootPanel::get()->addJSOnReadyDocument('$(\'#' . $this->id . '\').focus();');
		}

		if ($this->name != null)
			$str .= ' name="' . $this->name . '"';

		if ($this->style != null)
			$str .= ' class="' . $this->style . '"';

		if ($this->maxCharacters != null)
			$str .= ' size="' . $this->maxCharacters . '"';

		if ($this->maxLength != null)
			$str .= ' maxlength="' . $this->maxLength . '"';

		if ($this->value != null)
			$str .= ' value="' . htmlspecialchars($this->value) . '"';

		$str .= ' type="' . $this->getType() . '">';

		return $str;
	}
}
?>